package com.example.controller;

import com.example.entity.Result;
import com.example.entity.User;
import com.example.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private IUserService userService;

    /**
     * 返回到user/login.jsp
     * @return
     */
    @GetMapping("/login")
    public String loginView(){
        return "user/login";
    }

    @GetMapping("/send")
    @ResponseBody
    public Result send(String email, HttpSession session){
        User user = userService.login(email);
        if(user == null){
            return new Result("404","尚未注册",null);
        }


        SimpleMailMessage msg = new SimpleMailMessage();

        msg.setFrom("1903566209@qq.com");//邮件的发送方

        msg.setSubject("常州工学院验证码");


        //随机的验证码 - 100000 - 999999

        //java.lang.Math 数学工具类
        //static double random();//随机方法 [0.0,1.0)
        int n = (int) (Math.random()*900000+100000);

        //session作用域 - 存储数据的容器
        //生命周期 - 一次会话期间[从浏览器开始 - 浏览器退出]
        session.setAttribute("code",String.valueOf(n));

        msg.setText("您好,验证码是:"+n);

        msg.setTo(email);

        sender.send(msg);


        return new Result("200","验证码发送成功,注意查收",email);
    }

    @PostMapping("/logincl")
    @ResponseBody
    public Result logincl(String email,String code,HttpSession session){

        //根据email来查询User
        //controller调用service层
        User user = userService.login(email);

        //判断user是否存在..
        if(user == null){
            return new Result("404","抱歉,邮箱不存在!",null);
        }

        //user是存在的...
        //比较验证码
        String codes = (String) session.getAttribute("code");

        //字符串的比较实用equals...
        if(code.equals(codes)){
            session.setAttribute("user",user);
            return new Result("200","登录成功!",null);
        }

        return new Result("500","登录失败,验证码错误!",null);
    }

    @GetMapping("/exit")
    public String exit(HttpSession session){
        //清空session
        session.invalidate();
        //跳转到登录界面
        return "user/login";
    }

}
